package com.gking.centerSystem.controller.publicApi;

import com.gking.centerSystem.common.Result;
import com.gking.centerSystem.serviceUtils.DeepSeek.DeepSeekService;
import com.gking.centerSystem.serviceUtils.QWen.QWenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/ai")
public class AIController {

    @Resource
    private DeepSeekService deepSeekService;

    @Resource
    private QWenService qWenService;


    @GetMapping("/deepseek")
    public Result<String> directChatDeepSeek(@RequestParam String message) {
        try {
            if (message == null || message.trim().isEmpty()) {
                return Result.error("消息内容不能为空");
            }

            String aiResponse = deepSeekService.oneTimeSimpleQA(message);
            return Result.success(aiResponse);

        } catch (Exception e) {
            log.error("处理AI请求失败", e);
            return Result.error("抱歉，AI服务暂时不可用");
        }
    }

    @GetMapping("/qwen")
    public Result<String> directChatQWen(@RequestParam String message) {
        try {
            if (message == null || message.trim().isEmpty()) {
                return Result.error("消息内容不能为空");
            }

            String aiResponse = qWenService.oneTimeSimpleQA(message);
            return Result.success(aiResponse);

        } catch (Exception e) {
            log.error("处理AI请求失败", e);
            return Result.error("抱歉，AI服务暂时不可用");
        }
    }



    /**
     * 简化聊天接口 - 返回JSON格式
     */
    @PostMapping("/simple-chat")
    public ResponseEntity<Map<String, String>> simpleChat(@RequestBody Map<String, String> request) {
        try {
            String userMessage = request.get("message");

            if (userMessage == null || userMessage.trim().isEmpty()) {
                Map<String, String> errorResponse = new HashMap<>();
                errorResponse.put("error", "消息内容不能为空");
                return ResponseEntity.badRequest().body(errorResponse);
            }

            String aiResponse = deepSeekService.oneTimeSimpleQA(userMessage);

            Map<String, String> successResponse = new HashMap<>();
            successResponse.put("response", aiResponse);
            return ResponseEntity.ok(successResponse);

        } catch (Exception e) {
            log.error("处理AI请求失败", e);
            Map<String, String> errorResponse = new HashMap<>();
            errorResponse.put("error", "AI服务暂时不可用: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }


}